<!--
 * Description: Focal分配修改页面
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Qiuxue.Wu
 * Date: 2020/05/26 14:24
-->

<template>
  <div>
    <!--数据表单-->
    <el-form label-width="200px" :model="formData" size="mini">
      <el-row>
        <!--行号-->
        <el-col :span="24">
          <el-form-item :label="$t('FdData.NO')">
            <span>{{ currentRowNo }}</span>
          </el-form-item>
        </el-col>

        <!--Part Number-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.basePartNo') + '(Base)'">
            <span>{{ formData.basePartNo }}</span>
          </el-form-item>
        </el-col>

        <!--Part Number-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.newPartNo') + '(New)'">
            <span>{{ formData.newPartNo }}</span>
          </el-form-item>
        </el-col>
        <!--Size-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.baseSize') + '(Base)'">
            <span>{{ formData.baseSize }}</span>
          </el-form-item>
        </el-col>

        <!--Size-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.newSize') + '(New)'">
            <span>{{ formData.newSize }}</span>
          </el-form-item>
        </el-col>

        <!--Qty-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.baseQty') + '(Base)'">
            <span>{{ formData.baseQty }}</span>
          </el-form-item>
        </el-col>

        <!--Qty-->
        <el-col :span="12">
          <el-form-item :label="$t('FdData.newQty') + '(New)'">
            <span>{{ formData.newQty }}</span>
          </el-form-item>
        </el-col>

        <!--采购分类-->
        <el-col :span="24">
          <el-form-item :label="$t('FdData.commodity')">
            <el-select
              v-model="formData.commodity"
              clearable
              filterable
              :placeholder="$t('select.placeholder')"
              size="mini"
              style="width: 100%"
            >
              <el-option
                v-for="item in $store.getters['commodity/getCommodityCollectsByGcmFunction'](
                  formData.gcmFunction
                )"
                :key="item.name"
                :label="item.name"
                :value="item.name"
              >
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: #b4b4b4; font-size: 12px">
                  {{ item.gcmFunction }}
                </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <!--GCM Cost-->
        <el-col :span="24">
          <el-form-item :label="$t('FdData.costDown')">
            <el-input
              v-model="formData.costDown"
              v-precision:costDown="formData"
              type="number"
              @blur="handlerInput"
            />
          </el-form-item>
        </el-col>

        <!--GCM 报价备注-->
        <el-col :span="24">
          <el-form-item :label="$t('FdData.gcmCostRemark')">
            <el-input v-model="formData.gcmCostRemark" clearable :rows="3" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span style="display: block; text-align: center; margin-top: 5px">
      <!--取消按钮-->
      <el-button size="medium" type="danger" @click="cancel">
        {{ $t('button.cancel') }}
      </el-button>
      <!--保存按钮-->
      <el-button :loading="isRequesting" size="medium" type="primary" @click="save">
        {{ $t('button.save') }}
      </el-button>
    </span>
  </div>
</template>

<script>
import { fdCostSaveSingle } from '@/api/ecdfc/cost'

export default {
  name: 'EcEvaluationEdit',
  props: {
    inputData: {
      type: Object,
      default: () => {}
    },
    currentRowNo: {
      type: Number,
      default: 0
    },
    gcmOwnerCommodityMp: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      isRequesting: false,
      formData: {}
    }
  },
  created() {
    this.pageDataInit()
  },
  methods: {
    pageDataInit() {
      // 将props传入的值复制给formData的值
      this.formData = this.$deepCopy(this.inputData)
    },
    async save() {
      const commodityTrim = String(this.formData.commodity).trim()

      if (commodityTrim === '' || commodityTrim === 'null') {
        this.$message({
          message: 'Commodity必填',
          type: 'error',
          duration: 1500
        })
        return
      }

      const costDownTrim = String(this.formData.costDown).trim()

      if (costDownTrim === '' || costDownTrim === 'null') {
        this.$message({
          message: 'Cost Down必填',
          type: 'error',
          duration: 1500
        })
        return
      }

      try {
        this.isRequesting = true
        await fdCostSaveSingle(
          {
            commodity: this.formData.commodity,
            fdRowId: this.formData.fdRowId,
            costDown: this.formData.costDown,
            gcmCostRemark: this.formData.gcmCostRemark
          },
          { msgSuccess: true, msgError: true }
        )
        this.$emit('save', this.formData)
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    },
    cancel() {
      this.$emit('cancel')
    },
    handlerInput() {
      setTimeout(() => {
        const newGcmCost = this.formData.gcmCost
        const newQty = this.formData.newQty
        const basePrice = this.formData.basePrice
        const baseQty = this.formData.baseQty
        this.formData.gcmCostDelta = String(newGcmCost * newQty - basePrice * baseQty)
      }, 500)
    }
  }
}
</script>

<style scoped>
.el-form-item__content span {
  margin-left: 5px;
  color: blue;
}
</style>
